package com.dengdai.applibrary.base;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.dengdai.applibrary.R;
import com.dengdai.applibrary.annotation.InjectUtils;
import com.dengdai.applibrary.config.BaseViewInfo;
import com.dengdai.applibrary.utils.LogUtils;
import com.dengdai.applibrary.utils.Operation;
import com.dengdai.applibrary.utils.SystemBarTintManager;
import com.dengdai.applibrary.utils.ToastUtils;
import com.dengdai.applibrary.view.custom.TitleBarView;
import com.dengdai.applibrary.view.dialog.CustomProgressView;
import com.umeng.analytics.MobclickAgent;

/**
 * @说明 Activity基类
 * @作者 LY
 * @时间 15/12/15 09:24
 * @版权 Copyright(c) 2015 LY-版权所有
 */
public abstract class BaseActivity extends FragmentActivity implements IBaseActivity {
    public String TAG = this.getClass().getSimpleName();
    /**
     * 动画类型
     **/
    private int mAnimationType = NONE;
    private static Operation mOperation = null;
    private TitleBarView titleBarView;

    private CustomProgressView customProgressView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutResID());
        InjectUtils.initInjectedView(this);
        LogUtils.v(TAG, "onCreate");
        BaseApplication.addActivity(this);

        if (null != findViewById(MResource.getIdByName(getApplication(), "id", "id_iv_back"))) {
            findViewById(MResource.getIdByName(getApplication(), "id", "id_iv_back")).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }

        /**
         * 初始化公共控件
         */
        mOperation = new Operation(this);
        titleBarView = new TitleBarView(getWindow());
        {//状态栏修改
            // setTranslucentStatus();
        }
        /**
         * 初始化View
         */
        initView();

        /**
         * 业务操作
         */
        business(this);


    }

    /**
     * 返回按钮操作
     *
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
        resume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        destroy();
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    /**
     * 界面消失操作
     */
    @Override
    public void finish() {
        super.finish();
        switch (mAnimationType) {
            case IBaseActivity.LEFT_RIGHT:
                overridePendingTransition(0, BaseView.gainResId(this, BaseView.ANIM, BaseViewInfo.SLIDE_RIGHT_OUT));
                break;
            case IBaseActivity.TOP_BOTTOM:
                overridePendingTransition(0, BaseView.gainResId(this, BaseView.ANIM, BaseViewInfo.PUSH_UP_OUT));
                break;
            case IBaseActivity.FADE_IN_OUT:
                overridePendingTransition(0, BaseView.gainResId(this, BaseView.ANIM, BaseViewInfo.FADE_OUT));
                break;
        }
        mAnimationType = NONE;
    }

    /**
     * 获取共通操作
     *
     * @return 初始化的共同操作
     */
    public static Operation getOperation() {
        return mOperation;
    }

    /**
     * 获取头部控件
     *
     * @return 头部控件
     */
    public TitleBarView getTitleBarView() {
        return this.titleBarView;
    }

    @Override
    public void resume() {
    }

    @Override
    public void destroy() {

    }

    /**
     * 设置状态栏和标题栏统一
     */
    private void setTranslucentStatus() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window win = getWindow();
            WindowManager.LayoutParams winParams = win.getAttributes();
            final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
            winParams.flags |= bits;
            win.setAttributes(winParams);
            SystemBarTintManager tintManager = new SystemBarTintManager(this);
            // 激活状态栏设置
            tintManager.setStatusBarTintEnabled(true);
            // 激活导航栏设置
            tintManager.setNavigationBarTintEnabled(true);
            tintManager.setStatusBarTintResource(0);
            titleBarView.getTitleStatusBar().setVisibility(View.VISIBLE);
        } else {
            titleBarView.getTitleStatusBar().setVisibility(View.GONE);
        }
    }

    public void startProgressDialog() {
        if (customProgressView == null) {
            customProgressView = new CustomProgressView(this, R.style.CustomProgressDialog, "数据加载中请稍后...");
        }
        customProgressView.show();
    }

    public void startProgressDialog(String loadingMsg) {
        if (customProgressView == null) {
            customProgressView = new CustomProgressView(this, R.style.CustomProgressDialog, loadingMsg);
        }
        customProgressView.show();
    }

    public void stopProgressDialog() {
        if (customProgressView != null) {
            customProgressView.dismiss();
            customProgressView = null;
        }
    }

    public void showToastMsg(String tost) {
        if (!TextUtils.isEmpty(tost))
            ToastUtils.showToastMsg(this, tost);

    }
}
